package com.xposed.aliproxy.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.xposed.aliproxy.xposed.utils.XMerTradeNoRequestCallBack;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import org.apache.http.HttpHeaders;
import org.apache.http.cookie.SM;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;

public class XposeUtil {
    public static void xposedLog(String str, String str2) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("xposedlog_");
        stringBuilder.append(str);
        stringBuilder.append(str2);
        XposedBridge.log(stringBuilder.toString());
    }

    public static void sendBroadcaseMsg(Context context, String str) {
        Intent intent = new Intent("kaifu.com.keyinputtool.action.toast");
        intent.putExtra("message", str);
        context.sendBroadcast(intent);
    }

    public static String m2989a() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("888");
        stringBuilder.append(DateUtil.formatData("SSS"));
        stringBuilder.append(Utils.randomStr(13));
        return stringBuilder.toString();
    }

    public static String getMac() {
        Enumeration networkInterfaces;
        String str = null;
        try {
            networkInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            e.printStackTrace();
            networkInterfaces = null;
        }
        if (networkInterfaces == null) {
            return null;
        }
        while (networkInterfaces.hasMoreElements()) {
            try {
                String str2 = new String(((NetworkInterface) networkInterfaces.nextElement()).getHardwareAddress());
                if (str2 != null) {
                    str = str2;
                    break;
                }
                str = str2;
            } catch (Throwable th) {
                XposedBridge.log(th);
            }
        }
        return str;
    }

    public static String getDeviceID(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Activity.TELEPHONY_SERVICE);
        if (ContextCompat.checkSelfPermission(context, "android.permission.READ_PHONE_STATE") != 0) {
            return null;
        }
        return telephonyManager.getDeviceId();
    }

    @TargetApi(26)
    public static String getImei(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Activity.TELEPHONY_SERVICE);
        if (ContextCompat.checkSelfPermission(context, "android.permission.READ_PHONE_STATE") != 0) {
            return null;
        }
        return telephonyManager.getDeviceId();
    }

    /* renamed from: a */
    public static String m2992a(String str) {
        try {
            String c = XposeUtil.m3003c();
            if (!Utils.isNull(c)) {
                return new JSONObject(c).getString(str);
            }
        } catch (Throwable th) {
            XposedBridge.log(th);
        }
        return null;
    }

    public static void saveDeviceInfo(String str, String str2, String str3) {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("deviceId", str);
            jSONObject.put("imei", str2);
            jSONObject.put("mac", str3);
            str = jSONObject.toString();
            File file = new File("/sdcard/xposedfilepath/");
            if (!file.exists()) {
                file.mkdirs();
            }
            File file2 = new File(file, "config.txt");
            if (!file2.exists()) {
                file2.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(file2);
            fileWriter.write(str);
            fileWriter.flush();
            fileWriter.close();
        } catch (Throwable th) {
            XposedBridge.log(th);
        }
    }

    /* renamed from: c */
    private static String m3003c() {
        try {
            File file = new File("/sdcard/xposedfilepath/");
            if (!file.exists()) {
                file.mkdirs();
            }
            File file2 = new File(file, "config.txt");
            if (!file2.exists()) {
                file2.createNewFile();
            }
            return new BufferedReader(new FileReader(file2)).readLine();
        } catch (Throwable th) {
            XposedBridge.log(th);
            return null;
        }
    }

    /* renamed from: a */
    public static String m2993a(byte[] bArr) {
        if (bArr == null || bArr.length == 0) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        int length = bArr.length;
        for (int i = 0; i < length; i++) {
            stringBuilder.append(String.format("%02X:", new Object[]{Byte.valueOf(bArr[i])}));
        }
        if (stringBuilder.length() > 0) {
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
        return stringBuilder.toString();
    }

    /* renamed from: b */
    public static String m3000b(String str) {
        try {
            String c = Utils.str2Builder(String.valueOf(str.getBytes(HTTP.UTF_8).length));
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(c);
            stringBuilder.append(str);
            return stringBuilder.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /* renamed from: b */
    public static String m3001b(String str, String str2, String str3) {
        try {
            int indexOf = str.indexOf(str2) + str2.length();
            return str.substring(indexOf, str.indexOf(str3, indexOf));
        } catch (Exception e) {
            e.printStackTrace();
            XposeUtil.xposedLog("exception->", e.getLocalizedMessage());
            return "";
        }
    }

    public static String xposedAliSocialSdkContactService(ClassLoader classLoader) {
        try {
            Class findClass = XposedHelpers.findClass("com.alipay.mobile.framework.AlipayApplication", classLoader);
            Class findClass2 = XposedHelpers.findClass("com.alipay.mobile.personalbase.service.SocialSdkContactService", classLoader);
            return XposedHelpers.getObjectField(XposedHelpers.callMethod(XposedHelpers.callMethod(XposedHelpers.callMethod(XposedHelpers.callStaticMethod(findClass, "getInstance", new Object[0]), "getMicroApplicationContext", new Object[0]), "findServiceByInterface", findClass2.getName()), "getMyAccountInfoModelByLocal", new Object[0]), "loginId").toString();
        } catch (Throwable th) {
            XposedBridge.log(th);
            return null;
        }
    }

    public static String xposedAmnetUserInfo(ClassLoader classLoader) {
        XposedHelpers.callStaticMethod(XposedHelpers.findClass("com.alipay.mobile.common.transportext.biz.appevent.AmnetUserInfo", classLoader), "getSessionid", new Object[0]);
        Context context = (Context) XposedHelpers.callStaticMethod(XposedHelpers.findClass("com.alipay.mobile.common.transportext.biz.shared.ExtTransportEnv", classLoader), "getAppContext", new Object[0]);
        if (classLoader == null || XposedHelpers.callStaticMethod(XposedHelpers.findClass("com.alipay.mobile.common.helper.ReadSettingServerUrl", classLoader), "getInstance", new Object[0]) == null) {
            return null;
        }
        return (String) XposedHelpers.callStaticMethod(XposedHelpers.findClass("com.alipay.mobile.common.transport.http.GwCookieCacheHelper", classLoader), "getCookie", ".alipay.com");
    }

    /* renamed from: b */
    public static void m3002b(Context context, String str) {
        long currentTimeMillis = System.currentTimeMillis();
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date(currentTimeMillis));
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("https://mbillexprod.alipay.com/enterprise/simpleTradeOrderQuery.json?beginTime=");
        stringBuilder.append(currentTimeMillis - 7200000);
        stringBuilder.append("&limitTime=");
        stringBuilder.append(currentTimeMillis + 180000);
        stringBuilder.append("&pageSize=");
        stringBuilder.append(8);
        stringBuilder.append("&pageNum=1&channelType=ALL");
        String stringBuilder2 = stringBuilder.toString();
        HttpUtils httpUtils = new HttpUtils(15000);
        httpUtils.configResponseTextCharset("GBK");
        RequestParams requestParams = new RequestParams();
        requestParams.addHeader(SM.COOKIE, str);
        String str2 = HttpHeaders.REFERER;
        StringBuilder stringBuilder3 = new StringBuilder();
        stringBuilder3.append("https://render.alipay.com/p/z/merchant-mgnt/simple-order.html?beginTime=");
        stringBuilder3.append(format);
        stringBuilder3.append("&endTime=");
        stringBuilder3.append(format);
        stringBuilder3.append("&fromBill=true&channelType=ALL");
        requestParams.addHeader(str2, stringBuilder3.toString());
        httpUtils.send(HttpMethod.GET, stringBuilder2, requestParams, new XMerTradeNoRequestCallBack(context, str));
    }
}
